package com.freeSoft;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Properties;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

public class GenerateMain {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		JdbcTemplate jt = (JdbcTemplate)ctx.getBean("jdbcTemplate");
		Connection cn = jt.getDataSource().getConnection();
		DatabaseMetaData md =cn.getMetaData();
		ResultSet   mrs=md.getTables(null,null,null,new   String[]{"TABLE"})   ;  
		Properties prop = getRunTimeParameter();
		DealConfig dc = new DealConfig();
		dc.setProp(prop);
		dc.generateWEBINFConfig();
		CreateHeadJsp hjsp = new CreateHeadJsp();
		hjsp.setProp(prop);
		hjsp.createHeadJsp();
        while(mrs.next()){  
              System.out.println(mrs.getString(3));  
              String tableName = mrs.getString(3);
              String sql = " SELECT * FROM "+tableName;
              ResultSetMetaData rs =cn.createStatement().executeQuery(sql).getMetaData();
              CreateBeanFromMeta cb = new CreateBeanFromMeta();
              cb.setProp(prop);
              cb.setRs(rs);
              cb.setTableName(tableName);
              cb.generateBean();
              
              CreateDao cd = new CreateDao();
              cd.setProp(prop);
              cd.setTableName(tableName);
              cd.generateDaoInterface();
              
              CreateDaoImpl cdi= new CreateDaoImpl();
              cdi.setProp(prop);
              cdi.setRs(rs);
              cdi.setTableName(tableName);
              cdi.generateDaoImpl();
              
              DealConfig dcf = new DealConfig();
              dcf.setProp(prop);
              dcf.generateWEBINFConfig();
              
              DealSpringConfig dsc = new DealSpringConfig();
              dsc.setProp(prop);
              dsc.setTableName(tableName);
              dsc.addBeanToSpringConfig();
              
              CreateJsp cjp = new CreateJsp();
              cjp.setProp(prop);
              cjp.setRs(rs);
              cjp.setTableName(tableName);
              cjp.createJsp();
              
              CreateService cs = new CreateService();
              cs.setProp(prop);
              cs.generateServiceInterface();
              
              CreateServiceImpl csi = new CreateServiceImpl();
              csi.setProp(prop);
              csi.setTableName(tableName);
              csi.generateServiceImpl();
              
              CreateAction ca = new CreateAction();
              ca.setProp(prop);
              ca.setRs(rs);
              ca.setTableName(tableName);
              ca.generateAcion();
              
        }  
         
	}

	public static Properties getRunTimeParameter(){
		Properties prop = null;
		try{
		 prop = new Properties();// 灞炴�闆嗗悎瀵硅薄  
		 String osName = System.getProperty("os.name").toUpperCase() ;
		 String pathDir = ""; 
		 if (osName.indexOf("WINDOW") != -1){
			 pathDir = Class.class.getResource("/").getPath(); 
			 pathDir = pathDir.substring(1,pathDir.length());
		 }else {
			 pathDir = Class.class.getResource("/").getPath();
		 }
		 System.out.println( pathDir);
		 FileInputStream fis = new FileInputStream( pathDir +"generate.properties");
		 // 灞炴�鏂囦欢杈撳叆娴� 
		 prop.load(fis);// 灏嗗睘鎬ф枃浠舵祦瑁呰浇鍒癙roperties瀵硅薄涓�		 
		 fis.close();// 鍏抽棴娴�		
		 }catch(Exception e){
			e.printStackTrace();
		}
		return prop;
	}
}
